package com.ktdaddy.service.impl;

import com.ktdaddy.enums.WhaleErrorCode;
import com.ktdaddy.exception.BusinessRuntimeException;
import com.ktdaddy.manager.DeptManager;
import com.ktdaddy.pojo.Dept;
import com.ktdaddy.pojo.Permission;
import com.ktdaddy.request.CreateDeptReq;
import com.ktdaddy.request.UpdateDeptReq;
import com.ktdaddy.service.DeptService;
import com.ktdaddy.vo.DeptVO;
import com.ktdaddy.vo.PermVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: lujianhao
 * @Description: 部门接口实现
 * @Date: 2021/7/22 3:14 下午
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptManager deptManager;



    @Override
    public List<DeptVO> getDeptList(String deptName, Integer status) {
        List<DeptVO> result = new ArrayList<>();
        String rootName = null;
        String childName = null;

        if(deptManager.isRootNode(deptName)){
            rootName = deptName;
        }else {
            childName = deptName;
            //如果childName不为空，则查询其父节点名称
            if(!StringUtils.isEmpty(childName)){
                Dept dept = deptManager.queryParentByChildName(childName);
                if(Objects.isNull(dept)){
                    return result;
                }
                rootName = dept.getDeptName();
            }
        }

        List<DeptVO> deptVOS = deptManager.queryRootByDeptNameAndStatus(rootName,status);

        if(!StringUtils.isEmpty(rootName) && deptVOS.isEmpty()){
            //判断是否根节点，如果为非根节点，则返回当前树
            deptVOS = deptManager.queryParentByDeptNameAndStatus(rootName,status);

        }

        if(CollectionUtils.isEmpty(deptVOS)){
            return result;
        }

        for(DeptVO deptVO : deptVOS){
            fetchAndBuildChildren(deptVO,childName,status);
            result.add(deptVO);
        }
        return result;
    }

    @Override
    public void createDept(CreateDeptReq createDeptReq) {
        //校验当前的部门名称是否存在
        paramCheck(createDeptReq.getDeptName());
        deptManager.saveDept(createDeptReq);
    }

    @Override
    public void updateDept(UpdateDeptReq updateDeptReq) {
        //参数校验
        Dept dept = updateDeptParamCheck(updateDeptReq);
        deptManager.updateDept(updateDeptReq,dept);
    }

    @Override
    public void deleteDept(Integer deptId) {
        //参数校验
        deleteParamCheck(deptId);
        //删除
        deptManager.deleteById(deptId);
    }

    private void deleteParamCheck(Integer deptId) {
        //判断是否为子节点，如果其底下存在子节点则优先删除子节点
        List<Dept> deptList = deptManager.queryChildByParentId(deptId);
        if(!CollectionUtils.isEmpty(deptList)){
            throw new BusinessRuntimeException(WhaleErrorCode.ROOT_NODE_DELETE_ERROR);
        }
    }

    private Dept updateDeptParamCheck(UpdateDeptReq updateDeptReq) {
        //获取原始用户信息
        Dept dept = deptManager.queryById(updateDeptReq.getId());
        if(!Objects.isNull(dept) && !dept.getDeptName().equals(updateDeptReq.getDeptName())){
            paramCheck(updateDeptReq.getDeptName());
        }
        //根部门不支持选择其他父类部门
        if(dept.getParentId().equals(0) && !updateDeptReq.getParentDept().equals(0)){
            throw new BusinessRuntimeException(WhaleErrorCode.ROOT_NODE_NOT_SUPPORT_REMOVE);
        }
        return dept;
    }

    private void paramCheck(String deptName) {
        Dept dept = deptManager.queryByDeptName(deptName);
        if(!Objects.isNull(dept)){
            throw new BusinessRuntimeException(WhaleErrorCode.DEPT_NAME_EXIST);
        }
    }

    /**
     * 构建并获取子节点
     * @param deptVO
     */
    private void fetchAndBuildChildren(DeptVO deptVO,String deptName,Integer status) {
        List<DeptVO> children = deptManager.queryChildByDeptNameAndStatus(deptVO.getId(),deptName,status);
        if(!children.isEmpty()){
            for(DeptVO childDeptVO : children){
                fetchAndBuildChildren(childDeptVO,deptName,status);
            }
            deptVO.setChildren(children);
        }
    }
}
